begintownscript;

variables;
short choice;
short i;

body;

beginstate 0; //INIT_STATE
	//intro dialog
	if(get_flag(6,0) == 0) {
		play_sound(-99); //rattling chains
	
		force_view_center(24,37);
		relocate_character(0,24,37);
		relocate_character(1,23,38);
		relocate_character(2,24,38);
		relocate_character(3,25,38);
		force_instant_terrain_redraw();
		
		pause(5);
		
		reset_dialog();
		add_dialog_str(0,"As the inner gate closes behind you, the sounds of battle grow dim. Of course, you don't care that much, because now there are two gates between the Loyalists and you.",0);
		add_dialog_str(1,"The other side of that equation is that you're trapped in Copperpeak now. A few people look at you uncertainly as you get your bearings.",0);
		add_dialog_str(2,"You should probably find whoever's in charge here.",0);
		run_dialog(1);
		
		set_flag(6,0,1);
		}
	
	//groups
	add_range_to_group(6,12,1); //archers
	add_char_to_group(20,1);
	add_range_to_group(13,15,2); //guards
	add_char_to_group(26,2);
	add_range_to_group(29,31,2);
	add_range_to_group(16,19,3); //refugees
	
	//standard setup
	set_crime_tolerance(0); //low tolerance in a besieged city
	set_name(1001,"Archer");
	set_name(1002,"Guard");
	set_name(1003,"Refugee");
	set_character_pose(17,14); //#17 is asleep
	set_character_pose(18,14); //#18 is asleep
	set_character_pose(19,14); //#19 is asleep
	set_character_pose(20,14); //#20 is asleep
	set_name(21,"Commander Fisher");
	set_level(21,20);
	set_char_dialogue_pic(21,520,0);
	set_name(22,"Carli");
	set_char_dialogue_pic(22,525,0);
	set_name(23,"Tyler");
	set_char_dialogue_pic(23,526,0);
	set_character_pose(25,14); //#25 is asleep
	set_character_pose(26,14); //#26 is asleep
	
	
	//fix up the trapdoor
	if(get_flag(6,16) >= 2) {
		set_terrain(11,29,284);
		set_terrain(11,28,137);
		}
	
	
	force_instant_terrain_redraw();
	
	
break;

beginstate 1; //EXIT_STATE
break;

beginstate 2; //START_STATE
	//Checking for the lead pc each turn... not necessary here
	//pc_counter = 0;
	//while(char_ok(pc_counter) == 0) {
	//	pc_counter = pc_counter + 1;
	//	}
	//lead_char = pc_counter;
break;


beginstate 10; //fallen statue
	if(get_flag(6,2) == 1)
		end();
	
	reset_dialog();
	add_dialog_str(0,"This statue (or rather, what's left of it), appears to be constructed out of copper, fittingly enough.",0);
	add_dialog_str(1,"The plaque at the base has been damaged beyond legibility.",0);
	run_dialog(1);
	
	set_flag(6,2,1);
break;

beginstate 15; //refugee building
	if(get_flag(6,3) == 1)
		end();
	
	force_instant_terrain_redraw();
	pause(5);
	
	reset_dialog();
	add_dialog_str(0,"This building is full of sleeping people, probably refugees from the surrounding farms.",0);
	add_dialog_str(1,"One of them stirs as you enter, but they do not wake up.",0);
	run_dialog(1);
	
	set_flag(6,3,1);
break;

beginstate 20; //well
	if(get_flag(6,4) == 1)
		end();
	
	reset_dialog();
	add_dialog_str(0,"You're relieved to see that Copperpeak at least has a supply of fresh water. Hopefully, the Loyalists won't find a way to tamper with it.",0);
	run_dialog(1);
	
	set_flag(6,4,1);
break;

beginstate 25; //barracks
	if(get_flag(6,5) == 1)
		end();
	
	force_instant_terrain_redraw();
	pause(5);
	
	reset_dialog();
	add_dialog_str(0,"Judging from the sparse furnishings, this room looks like the barracks. It's quiet.",0);
	add_dialog_str(1,"There's only one soldier in here, and he's asleep.",0);
	run_dialog(1);
	
	set_flag(6,5,1);
break;

beginstate 30; //commander Fisher's office (door trigger)
	if(get_flag(6,6) == 1)
		end();
	
	force_instant_terrain_redraw();
	pause(5);
	
	reset_dialog();
	add_dialog_str(0,"As the door swings open, you hear a clinking sound, like glass on glass.",0);
	add_dialog_str(1,"Inside, you find a weary-looking soldier hunched over a desk. He has one hand on a bottle of ale, and one hand on the hilt of his sword.",0);
	add_dialog_str(2,"Of course, when he sees you, he relaxes. Slightly.",0);
	run_dialog(1);
	
	set_flag(6,6,1);
break;

beginstate 31; //commander Fisher's desk (specobj trigger)
	print_str_color("The Commander's desk is locked.",2);
break;

beginstate 35; //leaving Carli behind (code lovingly borrowed from VoDT)
	i = character_in_party(622);
	if (i >= 0) {
		reset_dialog();
		add_dialog_str(0,"Carli looks at you and asks,",0);
		add_dialog_str(1,"_Well, should I stay back?_",15);
		add_dialog_choice(0,"No, stay with me.");
		add_dialog_choice(1,"Yes, stay here for now.");
		choice = run_dialog(1);
		if (choice == 2) {
			erase_char(i);
			spawn_creature(22);
			}
		}
break;

beginstate 40; //Tyler's house (door trigger)
	if(get_flag(6,15) == 1)
		end();
	
	force_instant_terrain_redraw();
	pause(5);
	
	reset_dialog();
	add_dialog_str(0,"As the door swings open, a strong alchemical smell nearly knocks you off your feet. Amidst the haze inside, you think you can see a mage.",0);
	run_dialog(1);
	
	set_flag(6,15,1);
break;

beginstate 45; //trapdoor, bag of rocks (specobj)
	if(get_flag(6,10) < 2) {
		if(get_flag(6,16) == 0) {
			reset_dialog();
			add_dialog_str(0,"That's weird. These sacks are just full of rocks.",0);
			run_dialog(1);
			set_flag(6,16,1);
			}
		}
	else if(get_flag(6,16) < 2) {
		reset_dialog();
		if(get_flag(6,16) == 0) {
			add_dialog_str(0,"Well, these appear to be the sacks full of rocks that Tyler told you about.",0);
			add_dialog_str(1,"You dump out the bags on the ground, and find a trapdoor.",0);
			}
		else {
			add_dialog_str(0,"You return to the sacks full of rocks, now armed with Tyler's information.",0);
			add_dialog_str(1,"Sure enough, you dump out the bags, and find a trapdoor.",0);
			}
		run_dialog(1);
		set_flag(6,16,2);
		set_terrain(11,29,284);
		set_terrain(11,28,137);
		force_instant_terrain_redraw();
		}
break;

beginstate 50; //the trapdoor
	if(get_flag(6,16) < 2)
		end();
	block_entry(1);
	reset_dialog();
	add_dialog_str(0,"There's a trapdoor here, with a ladder underneath. Do you descend?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);
	if(choice == 2)
		move_to_new_town(11,21,27);
break;

beginstate 55; //ruined forge
	if(get_flag(6,17) == 1)
		end();
	
	reset_dialog();
	add_dialog_str(0,"This building looks like it used to be a forge, until it was nearly burned to the ground. A few pieces of debris are still smoking.",0);
	run_dialog(1);
	
	set_flag(6,17,1);
break;